import Vue from 'vue'
import vealPlugin from '@aev/vue'
import App from './App.vue'
import store from './store'
import router from './router'
import * as utils from './utils'
import service from './service.js'
import ElementUI from 'element-ui'
import nprogress from 'nprogress'
import VCharts from 'v-charts'
import VueQuillEditor from 'vue-quill-editor'

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import 'nprogress/nprogress.css'

// import 'element-ui/lib/theme-chalk/index.css'
import './assets/theme/index.css'
import './assets/css/reset.css'
import './assets/css/base.css'
import { checkActionAccess } from './access/check'
import VueDND from 'awe-dnd'
import * as nodes from './access/node'
import UserInfo from './component/base/UserInfo'
Vue.component('UserInfo', UserInfo)
Vue.mixin({
  methods: {
    $_isInit () {
      if (!this.$___init) {
        this.$___init = true
        return true
      }
    }
  }
})
Vue.use(VCharts)
Vue.use(vealPlugin, {})
Vue.use(VueQuillEditor)
Vue.prototype.$service = service(Vue)
Vue.prototype.$_utils = utils
Vue.use(ElementUI)
Vue.use(VueDND)
Vue.prototype.$accessCheck = checkActionAccess
Vue.prototype.$Access = nodes

const oldLoading = Vue.prototype.$loading
nprogress.configure({
  showSpinner: false,
  minimum: 0.05,
  trickleSpeed: 100
})
Vue.$loading = Vue.prototype.$loading = {
  lockStack: [], // 锁栈
  start () {
    if (!this.lockStack.length) {
      nprogress.start()
      this.loading = oldLoading({
        spinner: 'null',
        background: 'rgba(0,0,0,0)'
      })
    }
    this.lockStack.push(1)
  },
  done () {
    if (this.lockStack.length <= 1) {
      nprogress.done()
      this.loading.close()
    }
    this.lockStack.pop()
  }
}
// 文件下载
Vue.$_download = Vue.prototype.$_download = function (path = 'http://seopic.699pic.com/photo/50035/0520.jpg_wh1200.jpg', callback) {
  if (!process.env.IS_ELECTRON) {
    window.location.href = path
    return
  }
  return new Promise((resolve, reject) => {
    this.$electron.ipcRenderer.send('download', path)
    this.$electron.ipcRenderer.once('download-result', (e, result) => {
      if (typeof callback === 'function') {
        callback(result)
      }
      if (result) {
        this.$Message.success('文件下载成功')
        resolve(result)
      } else {
        reject(result)
      }
    })
  })
}

// 关闭当前路由跳转编辑路由
Vue.prototype.$closeCurrentGoEdit = function (goPath = '/saleOutWarehouseEdit/123') {
  if (typeof goPath !== 'string') return
  goPath = goPath.trim()
  goPath = goPath.charAt(0) === '/' ? goPath : `/${goPath}`
  const path = this.$route.path
  this.$store.dispatch('closeOneRoute', { path })
  setTimeout(() => {
    this.$router.push({ path: goPath }, () => {
      // this.$store.dispatch('closeOneRoute', { path })
    })
  }, 1)
}
// 张恒
Vue.prototype.$_utils.fixedToPlus = (val, len) => {
  if (len === void 0) {
    len = 4
  }
  const valStr = '' + val
  if (parseInt(valStr.substring(valStr.indexOf('.') + 1)) === 0) {
    len = 0
  }
  if (parseFloat(valStr) === parseInt(valStr)) len = 0
  const temp = parseFloat(valStr).toFixed(len)
  if (!isNaN(temp)) {
    return temp
  }
  if (val === '') {
    return ''
  }
  return 0
}
/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router: router(Vue),
  components: { App },
  template: '<App/>'
})
